<?php 
class ControllerProductAllCategories extends Controller {  
	public function index() {
		
			$this->language->load('product/allcategories');
			
			$this->load->model('catalog/category');
			
			$this->load->model('catalog/product');
			
			$this->load->model('tool/image'); 
			$this->data['categories'] = array();

		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home'),
       		'separator' => false
   		);	

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get("heading_title"),
			'href'      => $this->url->link('product/allcategories'),
       		'separator' => $this->language->get('text_separator')
   		);
		
		$this->data['heading_title'] = $this->language->get("heading_title");
		$this->document->setTitle($this->language->get("heading_title"));
			$categories = $this->model_catalog_category->getCategories(0);
			
			foreach ($categories as $category) {
				if ($category['top']) {
					// Level 2
					$children_data = array();
					
					$children = $this->model_catalog_category->getCategories($category['category_id']);
					$index=0;
					foreach ($children as $child) {
						$data = array(
							'filter_category_id'  => $child['category_id'],
							'filter_sub_category' => true
						);
						
						$product_total = $this->model_catalog_product->getTotalProducts($data);
										
						$children_data[] = array(
							'name'  => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $product_total . ')' : ''),
							'href'  => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id']),
							'category_id'=>$category['category_id'],
						);
						if($index==4)
						break;
						$index ++;
					}

					// FOR IMAGE ON REFINE SEARCH
					if ($category['image']) {
						$image = $this->model_tool_image->resize($category['image'], $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height'));
					} else {
						$image = false;
					}
				
					// Level 1
					$this->data['categories'][] = array(
						'name'     => $category['name'],
						'category_id'=>$category['category_id'],
						'children' => $children_data,
						'image'=>$image,
						'column'   => $category['column'] ? $category['column'] : 1,
						'href'     => $this->url->link('product/category', 'path=' . $category['category_id'])
					);
				}
			}
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/allcategories.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/product/allcategories.tpl';
			} else {
				$this->template = 'default/template/product/allcategories.tpl';
			}
			
			$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',

				'common/footer',
				'common/header'
			);
				
			$this->response->setOutput($this->render());
  	}
}
?>